package com.offcn.dao;





import com.offcn.utils.C3p0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class BaseDao<T>{
    private QueryRunner queryRunner = new QueryRunner(C3p0Utils.getDataSource());
    //增删改
    public  int dml(String sql ,Object...objs){
        int row = 0;
        try {
            row = queryRunner.update(sql, objs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }
    //查询单条记录
    public  T singleline(String sql ,Class<T> cla,Object...objs){
        T query = null;
        try {
            query = queryRunner.query(sql, new BeanHandler<>(cla), objs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return  query;
    }
    //查询多条记录
    public List<T> moreline(String sql,Class<T> cla  , Object...objs){
        List<T> ts = null;
        try {
            ts = queryRunner.query(sql, new BeanListHandler<>(cla), objs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ts;
    }
    //查询单列多行数据
    public  List<Object> singLeColumn(String sql ,Object...objs){
        List<Object> list = null;
        try {
            list = queryRunner.query(sql, new ColumnListHandler(), objs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
    //查询单一数据
    public Object singLeData(String sql ,Object...objs){
        Object o = null;
        try {
            o = queryRunner.query(sql, new ScalarHandler(), objs);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return o;
    }

}
